import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_yn_goods/config/app_color.dart';
import 'package:flutter_yn_goods/config/app_routes.dart';
import 'package:flutter_yn_goods/config/app_translations.dart';
import 'package:flutter_yn_goods/generated/assets.dart';
import 'package:flutter_yn_goods/utils/utils_other.dart';
import 'package:flutter_yn_goods/widgets/appbar/common_app_bar.dart';
import 'package:flutter_yn_goods/widgets/button/common_button.dart';
import 'package:flutter_yn_goods/widgets/image/auto_image.dart';
import 'package:flutter_yn_goods/widgets/login_input_item_view.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_yn_goods/widgets/widget_language_dialog.dart';
import 'package:get/get.dart';

import 'login_logic.dart';

class LoginPage extends StatelessWidget {
  final logic = Get.put(LoginLogic());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      body: Stack(
        children: [
          Column(
            children: [
              // SizedBox(
              //   height: ScreenUtil().statusBarHeight + 60.w,
              // ),
              CommonAppBar(title: '', actions: [
                GestureDetector(
                  onTap: () {
                    const WidgetLanguageDialog().show();
                  },
                  child: Container(
                    width: 36.w,
                    height: 36.w,
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(18.w),
                      boxShadow: [
                        BoxShadow(
                          color: Colors.black.withOpacity(0.05),
                          blurRadius: 4.w,
                          offset: Offset(0, 2.w),
                        ),
                      ],
                    ),
                    alignment: Alignment.center,
                    child: Text(
                      Get.locale?.languageCode == 'zh'
                          ? '中'
                          : Get.locale?.languageCode == 'en'
                          ? 'EN'
                          : 'VI',
                      style: TextStyle(
                        color: AppColor.primary,
                        fontSize: 14.sp,
                        fontWeight: FontWeight.w600,
                      ),
                    ),
                  ),
                ),
                16.horizontalSpace,
              ],),
              _buildLogoTips(),
              32.verticalSpaceFromWidth,
              TabBar(
                // tabAlignment: TabAlignment.center,
                indicator: UnderlineTabIndicator(
                  borderSide: const BorderSide(
                    width: 2,
                    color: AppColor.primary,
                  ),
                  insets: EdgeInsets.symmetric(horizontal: 20.w),
                ),
                labelColor: AppColor.color282828,
                unselectedLabelColor: AppColor.colorBDBDBD,
                unselectedLabelStyle:
                TextStyle(fontSize: 16.sp, fontWeight: FontWeight.w400),
                labelStyle:
                TextStyle(fontSize: 16.sp, fontWeight: FontWeight.bold),
                tabs: [
                  Text(
                    LocaleKeys.member_login.tr,
                  ),
                  Text(
                    LocaleKeys.agent_login.tr,
                  ),
                ],
                controller: logic.tabController,
              ),
              20.verticalSpaceFromWidth,
              Expanded(
                  child: TabBarView(controller: logic.tabController, children: [
                    _buildUser(),
                    _buildAgency()
                  ]))
            ],
          ),
          // Positioned(
          //   top: ScreenUtil().statusBarHeight + 20.w,
          //   right: 20.w,
          //   child: GestureDetector(
          //     onTap: () {
          //       const WidgetLanguageDialog().show();
          //     },
          //     child: Container(
          //       width: 36.w,
          //       height: 36.w,
          //       decoration: BoxDecoration(
          //         color: Colors.white,
          //         borderRadius: BorderRadius.circular(18.w),
          //         boxShadow: [
          //           BoxShadow(
          //             color: Colors.black.withOpacity(0.05),
          //             blurRadius: 4.w,
          //             offset: Offset(0, 2.w),
          //           ),
          //         ],
          //       ),
          //       alignment: Alignment.center,
          //       child: Text(
          //         Get.locale?.languageCode == 'zh'
          //             ? '中'
          //             : Get.locale?.languageCode == 'en'
          //                 ? 'EN'
          //                 : 'VI',
          //         style: TextStyle(
          //           color: AppColor.primary,
          //           fontSize: 14.sp,
          //           fontWeight: FontWeight.w600,
          //         ),
          //       ),
          //     ),
          //   ),
          // ),
        ],
      ),
    );
  }

  Widget _buildAgency() {
    return Column(
      children: [
        LoginInputItemView(
          title: LocaleKeys.account.tr,
          textEditingController: logic.accountAgEditController,
          maxLength: logic.accountLengthLimit,
          hintText: LocaleKeys.please_enter_account.tr,
          textChanged: (v) {
            logic.onTextChange();
          },
        ),
        16.verticalSpaceFromWidth,
        Obx(() {
          return LoginInputItemView(
            title: LocaleKeys.password.tr,
            textEditingController: logic.passworAGdEditController,
            rightChild: PasswordShowOrHideSwitch(
              onTap: () {
                logic.changePasswordState();
              },
              show: logic.showPassword.value,
            ),
            obscureText: !logic.showPassword.value,
            maxLength: logic.passwordLengthLimit,
            hintText: LocaleKeys.please_enter_password.tr,
            textChanged: (v) {
              logic.onTextChange();
            },
          );
        }),
        12.verticalSpaceFromWidth,
        Row(
          children: [
            GestureDetector(
                onTap: () {
                  logic.agreementAgRx.value = !logic.agreementAgRx.value;
                },
                behavior: HitTestBehavior.opaque,
                child: Obx(() {
                  return Container(
                    margin: EdgeInsets.only(left: 12.w),
                    width: 44,
                    height: 44,
                    alignment: Alignment.center,
                    child: Icon(
                      logic.agreementAgRx.value
                          ? Icons.check_circle
                          : Icons.check_circle_outline,
                      weight: 16.w,
                      color: logic.agreementAgRx.value
                          ? AppColor.primary
                          : AppColor.color999999,
                    ),
                  );
                })),

            Expanded(
                child: RichText(
                  // maxLines: 4,

                    text: TextSpan(
                        style: TextStyle(
                            height: 1.5,
                            fontSize: 12.sp,
                            color: AppColor.color282828),
                        children: [
                          TextSpan(
                            text: LocaleKeys.agree_terms.tr,
                          ),
                          TextSpan(
                              text: LocaleKeys.privacy_policy.tr,
                              style: const TextStyle(color: AppColor.primary),
                              recognizer: TapGestureRecognizer()
                                ..onTap = () {
                                  Get.toNamed(AppRoutes.agreementPage,
                                      arguments: UtilsOther.getPrivacyId() + 3);
                                }),
                          TextSpan(text: LocaleKeys.and.tr),
                          TextSpan(
                              text: LocaleKeys.registration_agreement.tr,
                              style: const TextStyle(color: AppColor.primary),
                              recognizer: TapGestureRecognizer()
                                ..onTap = () {
                                  Get.toNamed(AppRoutes.agreementPage,
                                      arguments: UtilsOther.getPrivacyId() + 4);
                                }),
                        ])))
            // Text('我已阅读并同意')
          ],
        ),
        const Spacer(),
        // 80.verticalSpaceFromWidth,
        Padding(
          padding: EdgeInsets.symmetric(horizontal: 37.w),
          child: Obx(() {
            return CommonButton.expend(
              title: LocaleKeys.login.tr,
              height: 52.w,
              // fontWeight: FontWeight.w600,
              fontSize: 16.sp,
              disableBackgroundColor: Colors.black.withOpacity(0.04),
              disableTextColor: AppColor.color999999,
              enable: logic.canAgLogin.value,
              onClick: logic.toLogin,
            );
          }),
        ),
        12.verticalSpaceFromWidth,
        Padding(
          padding: EdgeInsets.symmetric(horizontal: 37.w),
          child: CommonButton.expend(
            title: LocaleKeys.agent_register.tr,
            height: 52.w,
            textColor: AppColor.primary,
            backgroundColor: Colors.transparent,
            fontSize: 14.sp,
            fontWeight: FontWeight.w400,
            onClick: logic.toRegister,
          ),
        ),
      ],
    );
  }

  Widget _buildUser() {
    return Column(
      children: [
        LoginInputItemView(
          title: LocaleKeys.account.tr,
          textEditingController: logic.accountEditController,
          maxLength: logic.accountLengthLimit,
          hintText: LocaleKeys.please_enter_account.tr,
          textChanged: (v) {
            logic.onTextChange();
          },
        ),
        16.verticalSpaceFromWidth,
        Obx(() {
          return LoginInputItemView(
            title: LocaleKeys.password.tr,
            textEditingController: logic.passwordEditController,
            rightChild: PasswordShowOrHideSwitch(
              onTap: () {
                logic.changePasswordState();
              },
              show: logic.showPassword.value,
            ),
            obscureText: !logic.showPassword.value,
            maxLength: logic.passwordLengthLimit,
            hintText: LocaleKeys.please_enter_password.tr,
            textChanged: (v) {
              logic.onTextChange();
            },
          );
        }),
        12.verticalSpaceFromWidth,
        Row(children: [
          GestureDetector(
              onTap: () {
                logic.agreementRx.value = !logic.agreementRx.value;
              },
              behavior: HitTestBehavior.opaque,
              child: Obx(() {
                return Container(
                  margin: EdgeInsets.only(left: 12.w),
                  width: 44,
                  height: 44,
                  alignment: Alignment.center,
                  child: Icon(
                    logic.agreementRx.value
                        ? Icons.check_circle
                        : Icons.check_circle_outline,
                    weight: 16.w,
                    color: logic.agreementRx.value
                        ? AppColor.primary
                        : AppColor.color999999,
                  ),
                );
              })),
          Expanded(
              child: RichText(
                  text: TextSpan(
                      style: TextStyle(
                          fontSize: 12.sp,
                          height: 1.5,
                          color: AppColor.color282828),
                      children: [
                        TextSpan(
                          text: LocaleKeys.agree_terms.tr,
                        ),
                        TextSpan(
                            text: LocaleKeys.privacy_policy.tr,
                            style: const TextStyle(color: AppColor.primary),
                            recognizer: TapGestureRecognizer()
                              ..onTap = () {
                                Get.toNamed(AppRoutes.agreementPage,
                                    arguments: UtilsOther.getPrivacyId() + 3);
                              }),
                        TextSpan(text: LocaleKeys.and.tr),
                        TextSpan(
                            text: LocaleKeys.registration_agreement.tr,
                            style: const TextStyle(color: AppColor.primary),
                            recognizer: TapGestureRecognizer()
                              ..onTap = () {
                                Get.toNamed(AppRoutes.agreementPage,
                                    arguments: UtilsOther.getPrivacyId() + 4);
                              }),
                      ])))
        ]),
        // 80.verticalSpaceFromWidth,
        const Spacer(),
        Padding(
          padding: EdgeInsets.symmetric(horizontal: 37.w),
          child: Obx(() {
            return CommonButton.expend(
              title: LocaleKeys.login.tr,
              height: 52.w,
              // fontWeight: FontWeight.w600,
              fontSize: 16.sp,
              disableBackgroundColor: Colors.black.withOpacity(0.04),
              disableTextColor: AppColor.color999999,
              enable: logic.canLogin.value,
              onClick: logic.toLogin,
            );
          }),
        ),
        12.verticalSpaceFromWidth,
        Padding(
          padding: EdgeInsets.symmetric(horizontal: 37.w),
          child: CommonButton.expend(
            title: LocaleKeys.account_register.tr,
            height: 52.w,
            textColor: AppColor.primary,
            backgroundColor: Colors.transparent,
            fontSize: 14.sp,
            fontWeight: FontWeight.w400,
            onClick: logic.toRegister,
          ),
        ),
      ],
    );
  }

  Widget _buildLogoTips() {
    return Column(
      children: [
        AutoImage(
          Assets.appLogo,
          width: 80.w,
        ),
        12.verticalSpaceFromWidth,
        Text(
          LocaleKeys.login_title.tr,
          style: TextStyle(
              color: AppColor.color282828,
              fontSize: 14.sp,
              fontWeight: FontWeight.w500),
        ),
        12.verticalSpaceFromWidth,
        Text(
          LocaleKeys.login_subtitle.tr,
          style: TextStyle(
              color: AppColor.color999999,
              fontSize: 12.sp,
              fontWeight: FontWeight.w400),
        ),
      ],
    );
  }
}
